package cn.sdc.sfw.dao.model;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import cn.sdc.sfw.dao.types.DbType;
import cn.sdc.sfw.dao.types.DbTypes;

public class PropertyType {

	private Property Property_;
	private String propertyName_;
	private String columnName_;
	private DbType valueType_;
	private boolean primaryKey_ = false;
	private boolean persistent_ = true;

	public PropertyType(Property Property) {
		this(Property, DbTypes.OBJECT, Property.getPropertyName());
	}
	
	public PropertyType(Property Property, DbType dbType) {
		this(Property, dbType, Property.getPropertyName());
	}

	public PropertyType(Property Property, DbType dbType,
			String columnName) {

		Property_ = Property;
		propertyName_ = Property.getPropertyName();
		valueType_ = dbType;
		columnName_ = columnName;	
	}
	
	public PropertyType(String propertyName, DbType dbType) {
		propertyName_ = propertyName;
		valueType_ = dbType;
		columnName_ = propertyName;
	}

	public Property getProperty() {
		return Property_;
	}

	public String getPropertyName() {
		return propertyName_;
	}

	public String getColumnName() {
		return columnName_;
	}
	
	public void setColumnName(String columnName) {
		columnName_ = columnName;
	}

	public DbType getValueType() {
		return valueType_;
	}

	public boolean isPrimaryKey() {
		return primaryKey_;
	}

	public void setPrimaryKey(boolean primaryKey) {
		primaryKey_ = primaryKey;
	}

	public boolean isPersistent() {
		return persistent_;
	}

	public void setPersistent(boolean persistent) {
		persistent_ = persistent;
	}
	
	public static PropertyType[] createPropertyTypes(ResultSetMetaData rsmd)
			throws SQLException {

		int count = rsmd.getColumnCount();
		PropertyType[] propertyTypes = new PropertyType[count];
		for (int i = 0; i < count; ++i) {
			String propertyName = rsmd.getColumnLabel(i + 1);
			DbType dbType = DbTypes.getDbType(rsmd
					.getColumnType(i + 1));
			propertyTypes[i] = new PropertyType(propertyName, dbType);
		}
		return propertyTypes;
	}
}
